package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Accountreceivable;

@Repository(value = "accountReceivableDao")
@Transactional(readOnly = false)
public class AccountReceivableDaoImpl implements AccountReceivableDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	public List<Accountreceivable> getListAccountReceivable() {
		TypedQuery<Accountreceivable> q = em.createNamedQuery("Accountreceivable.findAll",Accountreceivable.class);
    	return q.getResultList();
	}

	public List<Accountreceivable> getListAccountReceivableByClientId(Integer clientId) {
		TypedQuery<Accountreceivable> q = em.createNamedQuery("Accountreceivable.findByClientId",Accountreceivable.class);
		q.setParameter("clientId",clientId);
    	return q.getResultList();
	}

	public List<Accountreceivable> getListAccountReceivableByPharmacyIdByUserId(Integer pharmacyId,Integer userId) {
		TypedQuery<Accountreceivable> q = em.createNamedQuery("Accountreceivable.findByPharmacyIdByUserId",Accountreceivable.class);
		q.setParameter("pharmacyId",pharmacyId);
		q.setParameter("userId",userId);
    	return q.getResultList();
	}
	
	public List<Accountreceivable> getListAccountReceivableByClientIdByPharmacyIdByUserId(Integer clientId,Integer pharmacyId,Integer userId) {
		TypedQuery<Accountreceivable> q = em.createNamedQuery("Accountreceivable.findByClientIdByPharmacyIdByUserId",Accountreceivable.class);
		q.setParameter("clientId",clientId);
		q.setParameter("pharmacyId",pharmacyId);
		q.setParameter("userId",userId);
    	return q.getResultList();
	}

	public void remove(Accountreceivable accountReceivable) {
		em.remove(em.getReference(Accountreceivable.class,accountReceivable.getAccountReceivableId()));
	}

	public void update(Accountreceivable accountReceivable) {
		em.merge(accountReceivable);
	}

	public void add(Accountreceivable accountReceivable) {
		em.persist(accountReceivable);
	}

	public Accountreceivable findAccountReceivableByAccountReceivableId(Integer accountReceivableId) {
		Accountreceivable res = null;
		try {
			res=em.find(Accountreceivable.class,accountReceivableId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}
}
